/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "RocketSpawner.hpp"
#include "World.hpp"
#include "ActorManager.hpp"
#include "Settings.hpp"
#include "Rocket.hpp"
#include "SpecialRocket.hpp"

namespace hny {

	RocketSpawner::RocketSpawner(World* world)
		:mWorld(world), mSpawnAccum(0.0f), mBonusRocketAccum(0.0f), mDualRocketAccum(0.0f)
	{
		mRocketSpawnDelay = Settings::GetInstance()->RocketSpawnDelay;
		mBonusRocketDelay = Settings::GetInstance()->BonusRocketSpawnDelay;
		mDualRocketDelay = Settings::GetInstance()->DualRocketSpawnDelay;
	}

	RocketSpawner::~RocketSpawner()
	{
	}

	void RocketSpawner::SpawnRockets(float dt) {

		mSpawnAccum += dt;
		mBonusRocketAccum += dt;
		mDualRocketAccum += dt;

		// Is it time to spawn another regular rocket?
		if(mSpawnAccum > mRocketSpawnDelay) {
			mSpawnAccum = 0.0f;

			float rocketVelocity = Settings::GetInstance()->RocketVelocity;
			int score = Settings::GetInstance()->RocketScore;

			hgeVector start, dir;
			FindTrajectory(start, dir);
			
			hgeVector vel = dir * rocketVelocity;

			hgeParticleSystemInfo pi = Settings::GetInstance()->RocketParticleInfo;

			// Insert the rocket into the world
			ActorType rocket(new Rocket(mWorld, start, vel, score, pi));
			ActorManager::GetInstance()->AddActor(rocket);
		}

		// Is it time to spawn another bonus rocket?
		if(mBonusRocketAccum > mBonusRocketDelay) {
			mBonusRocketAccum = 0.0f;

			hgeVector start, dir;
			FindTrajectory(start, dir);

			float rocketVelocity = Settings::GetInstance()->BonusRocketVelocity;
			int score = Settings::GetInstance()->BonusRocketScore;

			hgeVector vel = dir * rocketVelocity;

			hgeParticleSystemInfo rpi = Settings::GetInstance()->BonusRocketParticleInfo;

			// Insert the rocket into the world
			ActorType rocket(new Rocket(mWorld, start, vel, score, rpi));
			ActorManager::GetInstance()->AddActor(rocket);
		}

		// Is it time to spawn another dual rocket?
		if(mDualRocketAccum > mDualRocketDelay) {
			mDualRocketAccum = 0.0f;

			hgeVector start, dir;
			FindTrajectory(start, dir);

			float rocketVelocity = Settings::GetInstance()->DualRocketVelocity;
			int score = Settings::GetInstance()->DualRocketScore;

			hgeVector vel = dir * rocketVelocity;

			hgeParticleSystemInfo rpi = Settings::GetInstance()->DualRocketParticleInfo;

			// Insert the rocket into the world
			ActorType rocket(new SpecialRocket(mWorld, start, vel, score, rpi));
			ActorManager::GetInstance()->AddActor(rocket);
		}
	}

	void RocketSpawner::FindTrajectory(hgeVector& start, hgeVector& dir)
	{
		int screenWidth = 1024;
		int screenHeight = 768;

		// Find spawning position
		unsigned int ws = (unsigned int) ( ((double)rand() / (double)RAND_MAX) * screenWidth + 1);
		unsigned int we;

		we  = (unsigned int) ( ((double)rand() / (double)RAND_MAX) * screenWidth + 1);

      // Find flight direction
		start = hgeVector((float)ws, (float)screenHeight);
		hgeVector end((float)we, 0.0);

		dir = end - start;
		dir.Normalize();
		
	}
}